package com.instpower.things.helper;

import com.instpower.things.transport.DataBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @ClassName Utils
 * @Author: hellmash@gmail.com
 * @Date: 2020/2/25
 * @Version: 1.0.0
 * @Description: 工具类
 */
public class Utils {

    private static final Logger logger = LoggerFactory.getLogger(Utils.class);

    /**
     * 打印报文内容,方便调试
     * @param bytes
     * @param tips
     */
    public static void printFormatBytes(byte[] bytes, String tips) {
        StringBuilder stb = new StringBuilder();
        for (int j = 0; j < bytes.length; j++) {
            stb.append(" " + String.format("%02x", bytes[j]));
        }
        logger.info(tips + ":0x|" + stb.toString() + " |");
    }


    /**
     * ip解析
     * @param ipAddress "192.168.0.1"
     * @return
     */
    public static DataBuffer parseIp(String ipAddress){
        DataBuffer buffer = new DataBuffer(4);
        String[] ipList = ipAddress.split("\\.");
        if(ipList.length != 4)
            return null;
        for(String ip : ipList)
            buffer.writeByte(Short.parseShort(ip));
        return buffer;
    }

}
